﻿using Orchard.UI.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NineSeas.Services
{
    public class PaginationInfo : PagerParameters 
    {
        private const int _defaultPageSize = 10;

        public PaginationInfo(): this(0)
        {

        }

        public PaginationInfo(int currentPage): this(currentPage, _defaultPageSize)
        {
        }

        public PaginationInfo(int currentPage, int pageSize)
        {
            this.PageSize = pageSize;
            this.CurrentPage = currentPage;
            this.TotalRecords = 0;

            AdditionalData = new Dictionary<string, string>();
        }
        public PaginationInfo(int? currentPage, int? pageSize)
        {
            this.PageSize = pageSize;
            this.CurrentPage = currentPage;
            this.TotalRecords = 0;

            AdditionalData = new Dictionary<string, string>();
        }

        public new int? PageSize
        {
            get { return base.PageSize; }
            set
            {
                if (value != null)
                {
                    base.PageSize = value;
                }
            }
        }

        public int? CurrentPage
        {
            get { return Page - 1; }
            set { Page = value + 1; }
        }
        public int TotalRecords { get; set; }

        public int TotalPages
        {
            get
            {
                if (PageSize.HasValue && PageSize.Value != 0)
                {
                    var totalPages = TotalRecords / PageSize.Value;
                    if (TotalRecords % PageSize.Value != 0)
                    {
                        totalPages++;
                    }
                    return totalPages;
                }
                else
                {
                    return 0;
                }

            }
        }

        public int StartIndex
        {
            get
            {
                return (CurrentPage.HasValue && PageSize.HasValue) ? (CurrentPage.Value * PageSize.Value) : 0;
            }
        }

        public IDictionary<string, string> AdditionalData { get; set; }
    }
}